<template>
  <div class="songer">
    <!-- 照片墙 -->
    <div class="photo">
      <div class="top">
        <div class="left" @click="goBack">
          <van-icon name="arrow-left" />
        </div>

        <div class="right" @click="showShare = true">
          <van-icon name="share-o" />
        </div>
      </div>

      <!-- 分享面板 -->
      <van-share-sheet
        v-model="showShare"
        title="立即分享给好友"
        :options="options"
      />

      <van-image :src="songerartist.cover" lazy-load>
        <template v-slot:loading>
          <van-loading type="spinner" size="20" />
        </template>
      </van-image>
    </div>

    <!-- 个人信息 -->
    <div class="data">
      <!-- 头像 -->
      <div class="head">
        <div class="img" v-if="songerartist.cover">
          <van-image :src="songerartist.cover" lazy-load>
            <template v-slot:loading>
              <van-loading type="spinner" size="20" />
            </template>
          </van-image>
        </div>

        <!-- 名字 -->
        <div class="name" v-if="songerartist.name">
          <div class="namebox">
            {{ songerartist.name }}
          </div>
          <div class="icon" v-if="songidentify.imageUrl">
            <img :src="songidentify.imageUrl" alt="" />
          </div>
        </div>

        <div class="txt" v-if="songidentify.imageDesc">
          {{ songidentify.imageDesc }}
        </div>

        <div class="follow" v-if="followshow === false" @click="addFollow">
          <div class="btn">+ 关注</div>
        </div>

        <div class="follow" v-else @click="cancelFollow">
          <div class="btn add">已关注</div>
        </div>
      </div>
    </div>

    <!-- 歌手内容列表 -->
    <van-tabs v-model="active" swipeable sticky>
      <van-tab title="主页">
        <div class="tablist">
          <div class="tabtitle">艺人百科</div>

          <div class="tabicon">
            <div
              class="tabimg"
              v-if="songidentify.imageDesc && songidentify.imageUrl"
            >
              <img :src="songidentify.imageUrl" alt="" />
            </div>
            {{ songidentify.imageDesc }}
          </div>

          <div class="brief" v-if="songerartist.briefDesc">
            简介：{{ songerartist.briefDesc }}
          </div>
        </div>
      </van-tab>

      <van-tab title="歌曲">
        <div class="tablist">
          <div class="tabtitle">
            <van-icon name="play-circle-o" />
            热门歌曲50
          </div>

          <!-- 歌曲列表 -->
          <div
            class="tabbox"
            v-for="(item, index) in songsData"
            :key="index"
            @click="
              playAudio(
                item.id,
                item.al.picUrl,
                item.name,
                item.ar[0].name,
                item.ar[0].id
              )
            "
          >
            <!-- 封图 -->
            <div class="sealing">
              <van-image :src="item.al.picUrl" lazy-load>
                <template v-slot:loading>
                  <van-loading type="spinner" size="20" />
                </template>
              </van-image>
            </div>

            <!-- 名字 -->
            <div class="tabname">
              <div class="tabsongname">{{ item.name }}</div>
              <div class="tabtxt">
                <div class="textbox">SQ</div>
                {{ item.ar[0].name }}
              </div>
            </div>

            <div class="play">
              <van-icon
                :name="
                  audioId !== item.id
                    ? 'play-circle-o'
                    : isPaused
                    ? 'play-circle-o'
                    : 'pause-circle-o'
                "
              />
            </div>

            <div class="share" @click="showShare = true">
              <van-icon name="ellipsis" />
            </div>
          </div>

          <!-- 分享面板 -->
          <van-share-sheet
            v-model="showShare"
            title="立即分享给好友"
            :options="options"
          />
        </div>

        <!-- 播放器 -->
        <Playbox></Playbox>
      </van-tab>

      <van-tab title="专辑">
        <div class="tablist">
          <div
            class="tabbox"
            v-for="(item, index) in songsalbum"
            :key="index"
            @click="viewAlbum(item.id)"
          >
            <div class="sealingmap">
              <van-image :src="item.blurPicUrl" lazy-load>
                <template v-slot:loading>
                  <van-loading type="spinner" size="20" />
                </template>
              </van-image>
            </div>

            <div class="tabname">
              <div class="tabsongname">{{ item.name }}</div>
              <div class="maptxt">
                {{ item.publishTime | formatDate }} {{ item.size }}首
              </div>
            </div>
          </div>
        </div>
      </van-tab>

      <van-tab title="视频">
        <!-- 视频播放 -->
        <div class="video-box" v-for="(item, index) in mvData" :key="index">
          <video class="mv" :src="item.mvUrl" controls="controls"></video>

          <div class="mvtext">
            <div class="mvname">{{ item.name }}</div>
            <div class="mvtime">{{ item.publishTime }}</div>
            <div class="playnum">
              播放次数：<span>{{ item.playCount }}</span>
            </div>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import Playbox from "../components/Playbox.vue";
import { mapState, mapMutations } from "vuex";
export default {
  name: "Songer",

  components: {
    Playbox,
  },

  data() {
    return {
      active: 0,

      followshow: false,

      // 分享面板
      showShare: false,
      options: [
        [
          { name: "微信", icon: "wechat" },
          { name: "朋友圈", icon: "wechat-moments" },
          { name: "微博", icon: "weibo" },
          { name: "QQ", icon: "qq" },
        ],
        [
          { name: "复制链接", icon: "link" },
          { name: "分享海报", icon: "poster" },
          { name: "二维码", icon: "qrcode" },
          { name: "小程序码", icon: "weapp-qrcode" },
        ],
      ],

      //歌手id
      id: "",

      //歌手详情数据
      songerData: [],
      songuser: [],
      songerartist: [],
      songidentify: [],

      //歌手歌曲数据
      songsData: [],

      //歌手专辑数据
      songsalbum: [],

      //MV数据
      mvData: [],
    };
  },

  computed: {
    ...mapState([
      "audioId",
      "audioEle",
      "isPaused",
      "songName",
      "songerName",
      "singerId",
      "audioComment",
      "Followsinger",
    ]),
  },

  created() {
    this.id = this.$route.params.id;

    //获取歌手详情
    this.getSongerDetail();

    //获取歌手歌曲
    this.getArtists();

    //获取歌手专辑
    this.getAlbum();

    // 获得歌手mv
    this.getMv();
  },

  methods: {
    //返回上一级
    goBack() {
      this.$router.go(-1);
    },

    //点击关注歌手
    addFollow() {
      if (this.$cookies.get("uid") === null) {
        this.$toast("请先登录");
      } else {
        this.followshow = true;
        let Follow = {
          name: this.songerartist.name,
          img: this.songerartist.cover,
          id: this.songerartist.id,
          text: this.songidentify.imageDesc,
          follow: true,
        };

        this.addSinger(Follow);

        // 存放在本地储存中
        localStorage.setItem("Followsinger", JSON.stringify(this.Followsinger));
      }
    },

    //取消关注
    cancelFollow() {
      this.followshow = false;
    },

    //mutations
    ...mapMutations(["changeStateProperty", "addSinger"]),

    //获取歌手详情
    getSongerDetail() {
      this.axios({
        method: "get",
        url: "/artist/detail",
        params: {
          id: this.id,
        },
      })
        .then((res) => {
          this.songerData = res.data.data;
          this.songuser = this.songerData.user;
          this.songerartist = this.songerData.artist;
          this.songidentify = this.songerData.identify;
        })
        .catch((err) => {
          
        });
    },

    //获取歌手歌曲
    getArtists() {
      this.axios({
        method: "get",
        url: "/artists",
        params: {
          id: this.id,
        },
      })
        .then((res) => {
          this.songsData = res.data.hotSongs;
        })
        .catch((err) => {
          
        });
    },

    //获取歌手专辑
    getAlbum() {
      this.axios({
        method: "get",
        url: "/artist/album",
        params: {
          id: this.id,
        },
      })
        .then((res) => {
          this.songsalbum = res.data.hotAlbums;
        })
        .catch((err) => {
          
        });
    },

    //查看专辑页面
    viewAlbum(id) {
      this.$router.push({ name: "Album", params: { id } });
    },

    // 获得歌手mv
    getMv() {
      this.axios({
        method: "get",
        url: "/artist/mv",
        params: {
          id: this.id,
        },
      })
        .then((res) => {
          res.data.mvs.forEach((v) => {
            //获取mv地址
            this.axios({
              method: "get",
              url: "/mv/url",
              params: {
                id: v.id,
              },
            })
              .then((res) => {
                let mvData = [
                  {
                    name: v.name,
                    playCount: v.playCount,
                    publishTime: v.publishTime,
                    mvUrl: res.data.data.url,
                  },
                ];

                mvData.forEach((i) => {
                  this.mvData.push(i);
                });
              })
              .catch((err) => {
                
              });
          });
        })
        .catch((err) => {
          
        });
    },

    // 点击播放音频
    playAudio(id, picUrl, songName, songerName, singerId) {
      if (id === this.audioId) {
        if (!this.audioEle.paused) {
          //暂停歌曲
          this.audioEle.pause();
          this.changeStateProperty({ key: "isPaused", value: true });
        } else {
          //播放歌曲
          this.audioEle.play();
          this.changeStateProperty({ key: "isPaused", value: false });
        }
        return;
      }

      //获取音频url
      this.axios({
        method: "get",
        url: "/song/url",
        params: {
          id,
        },
      })
        .then((res) => {
          let audioUrl = res.data.data[0].url;
          this.changeStateProperty({ key: "audioUrl", value: audioUrl });
          this.changeStateProperty({ key: "audioId", value: id });
          //获取暂停状态
          this.changeStateProperty({ key: "isPaused", value: false });
        })
        .catch((err) => {
          
        });

      //获取播放图片
      this.changeStateProperty({ key: "picUrl", value: picUrl });

      this.changeStateProperty({ key: "songName", value: songName });
      this.changeStateProperty({ key: "songerName", value: songerName });
      this.changeStateProperty({ key: "singerId", value: singerId });

      //获取专辑评论数据
      this.axios({
        method: "get",
        url: "/comment/music",
        params: {
          id,
        },
      })
        .then((res) => {
          this.changeStateProperty({
            key: "audioComment",
            value: res.data.hotComments,
          });
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
// 照片墙
.photo {
  width: 100%;
  height: 300px;
  box-sizing: border-box;
  &/deep/.van-image__error {
    top: 70px;
    left: 190px;
  }
  &/deep/.van-loading {
    top: 70px;
    left: 190px;
  }
}

.photo {
  &/deep/ .van-image__img {
    width: 100%;
    position: relative;
    bottom: 40px;
    z-index: -99;
  }
}

.top {
  display: flex;
  padding: 10px 20px;
}

.left {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #fff;
}

.right {
  width: 20px;
  height: 20px;
  font-size: 20px;
  margin-left: auto;
  color: #fff;
}

//个人信息
.data {
  width: 86%;
  padding: 10px;
  border-radius: 10px;
  background-color: #fff;
  margin: auto;
  position: relative;
  bottom: 40px;
  display: flex;
  flex-wrap: wrap;
}

.head {
  width: 100%;
}

.img {
  width: 70px;
  border-radius: 50%;
  margin: auto;
  margin-top: -46px;
}

.img {
  &/deep/ .van-image__img {
    width: 70px;
    height: 70px;
    border-radius: 50%;
  }
}

.name {
  width: 100%;
  display: flex;
  align-items: center;
}

.namebox {
  margin: auto;
  padding-top: 10px;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  padding-left: 16px;
  padding-bottom: 4px;
}

.icon {
  width: 18px;
  height: 18px;
  position: relative;
  right: 90px;
  padding-top: 6px;
}

.icon img {
  width: 18px;
  height: 18px;
}

.txt {
  width: 100%;
  text-align: center;
  color: #9f9f9f;
}

.follow {
  width: 100%;
  padding-top: 10px;
}

.btn {
  width: 60px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  margin: auto;
  border-radius: 14px;
  background-color: #ff3a3a;
  color: #fff;
  font-weight: 600;
}

.add {
  width: 60px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  margin: auto;
  font-weight: 600;
  border-radius: 14px;
  border: 1px solid #fe3c30;
  color: #fe3c30;
  background-color: #fff;
}

// 歌手内容列表
/deep/.van-tabs {
  bottom: 30px;
}

/deep/.van-tab--active {
  font-weight: 600;
}

//主页
.tablist {
  width: 86%;
  background-color: #fff;
  border-radius: 14px;
  padding: 10px 16px;
  margin: auto;
  display: flex;
  flex-wrap: wrap;
  margin-top: 10px;
}

.tabtitle {
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  padding: 4px 0;
}

.tabicon {
  width: 100%;
  display: flex;
  align-items: center;
  color: #9f9f9f;
  letter-spacing: 1px;
}

.tabimg {
  width: 20px;
  height: 20px;
  padding: 0 6px 6px 4px;
}

.tabimg img {
  width: 20px;
  height: 20px;
}

//歌曲
.tabbox {
  width: 100%;
  display: flex;
  align-items: center;
  margin-top: 10px;
}

.sealing {
  width: 40px;
  height: 40px;
}

.sealing {
  &/deep/ .van-image__img {
    width: 40px;
    height: 40px;
  }
}

.tabname {
  width: 58%;
  display: flex;
  flex-wrap: wrap;
  padding-left: 6px;
}

.tabsongname {
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  padding-left: 10px;
}

.tabtxt {
  display: flex;
  color: #9f9f9f;
  align-items: center;
}

.textbox {
  width: 30px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border: 1px solid red;
  color: red;
  transform: scale(0.5);
}

.play {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 40px;
}

.share {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: auto;
}

//专辑
.sealingmap {
  width: 50px;
  height: 50px;
  border-radius: 10px;
}

.sealingmap {
  &/deep/ .van-image__img {
    width: 50px;
    height: 50px;
    border-radius: 10px;
  }
}

.maptxt {
  width: 100%;
  text-align: left;
  padding-left: 10px;
}

// 视频播放
.video-box {
  width: 100%;
  background-color: #fff;
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
  padding: 16px 10px;
}

.mv {
  width: 100%;
  margin: auto;
  display: block;
  border-radius: 14px;
}

.mvtext {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
}

.mvname {
  width: 100%;
  font-size: 18px;
  font-weight: 600;
  padding-bottom: 4px;
}

.mvtime {
  width: 100%;
  color: #9f9f9f;
}

.playnum {
  width: 100%;
  & span {
    color: #9f9f9f;
  }
}
</style>